class DomUtils {
    static createDom(name,styles,classNames) {
        let dom = document.createElement(name);
        for (let i in styles) {
            dom.style[i] = styles[i];
        }
        dom.classList.add(classNames)
        return dom;
    }
    static buildCanvas(dom) {
        let id = OtherUtils.RandomId();
        dom.innerHTML = `<canvas id="${id}" width="${dom.offsetWidth}" height="${dom.offsetHeight}"></canvas>`;
        let canvas = dom.children[0];
        return {
            canvas: canvas,
            ctx: canvas.getContext('2d')
        };
    }
    static GetElementsByClassName(dom,className) {
        let cn = dom.getElementsByClassName(className);
        let arr = [];
        for (let i = 0;i < cn.length;i++) {
            arr.push(cn[i]);
        }
        return arr;
    }
    static OnResizeFns = [];
    static OnResize() {
        let _onresize = () => {};
        if (window.onresize) {
            _onresize = window.onresize.bind(window);
        }
        window.onresize = function () {
            _onresize();
            DomUtils.OnResizeFns.forEach(f => f());
        }
    }
    static AddOnResize(fn) {
        DomUtils.OnResizeFns.push(fn);
    }
    // 绑定拖动
    // locationUpdate = (一开始的位置，当前位置，页面宽度，节点，状态)
    static BandDrag(sliderDom,leftPartDom,rightPartDom,overFn) {
        // let drag = false;
        let width = 0;
        overFn = overFn || (() => {});
        let updateLocation = (x) => {
            // let startX = e.pageX;
            let cwidth = parseInt(x / width * 10000) / 100;
            leftPartDom.style.width = `${cwidth}%`;
            rightPartDom.style.width = `${100 - cwidth}%`;
        }
        let release = () => {
            sliderDom.onmouseup = sliderDom.onmousemove = null;
            document.body.onmouseup = document.body.onmousemove = null;
            overFn();
        }
        sliderDom.onmousedown = function (e) {
            width = document.body.offsetWidth;
            document.body.onmousemove = function (e) {
                updateLocation(e.pageX);
            }
            sliderDom.onmousemove = function (e) {
                updateLocation(e.pageX);
            }
            sliderDom.onmouseup = function (e) {
                updateLocation(e.pageX);
                release();
            }
            document.body.onmouseup = function () {
                release();
            }
        };
    }
}
